package net.csdn.business.discuss.api.listener;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import net.csdn.business.discuss.api.event.DiscussActivityApplicationEvent;
import net.csdn.business.discuss.api.model.dto.*;
import net.csdn.business.discuss.api.service.IDiscussActivityService;
import net.csdn.business.discuss.api.service.INotifyService;
import net.csdn.business.discuss.common.config.AsyncThreadPoolConfig;
import net.csdn.business.discuss.common.model.dto.EmailPushDiscussDTO;
import net.csdn.business.discuss.feign.api.NotifyFeignClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @ClassName DIscussActivityListener
 * @Description TODO
 * @Author zhangyl
 * @Date 2023/7/22 11:38
 */

@Slf4j
@Component
public class DiscussActivityListener {

    @Autowired
    private IDiscussActivityService discussActivityService;

    @Autowired
    private INotifyService notifyService;

    @EventListener
    @Async(AsyncThreadPoolConfig.ASYNC_EXECUTOR_POOL_NAME)
    public void dealDiscussActivity(DiscussActivityApplicationEvent event) {
        DiscussActivityEventDTO eventSource=(DiscussActivityEventDTO)event.getSource();
        NewDiscussActivityDTO params=new NewDiscussActivityDTO();
        switch (eventSource.getAction()){
            case CREATE_DISCUSS:
                DiscussCreateActivityDTO createActivity=JSON.parseObject(eventSource.getPayload(),DiscussCreateActivityDTO.class);
                params.setEvent(eventSource.getAction().getCode());
                params.setDiscussId(createActivity.getDiscussId());
                params.setUid(createActivity.getActionUid());
                params.setCreatedDate(createActivity.getCreatedDate());
                discussActivityService.saveDiscussActivity(params);
                //发送通知
                notifyService.sendDiscussCreate(createActivity);
                break;
            case UPDATE_DISCUSS:
                BaseActivityDTO updateActivity=JSON.parseObject(eventSource.getPayload(),BaseActivityDTO.class);
                params.setEvent(eventSource.getAction().getCode());
                params.setDiscussId(updateActivity.getDiscussId());
                params.setUid(updateActivity.getActionUid());
                params.setCreatedDate(updateActivity.getCreatedDate());
                discussActivityService.saveDiscussActivity(params);
                //发送通知
                notifyService.sendDiscussUpdate(updateActivity);
                break;
            case CREATE_COMMENT:
                CommentCreateActivityDTO commentActivity=JSON.parseObject(eventSource.getPayload(), CommentCreateActivityDTO.class);
                params.setEvent(eventSource.getAction().getCode());
                params.setDiscussId(commentActivity.getDiscussId());
                params.setUid(commentActivity.getActionUid());
                params.setCreatedDate(commentActivity.getCreatedDate());
                discussActivityService.saveDiscussActivity(params);
                //发送通知
                notifyService.sendDiscussCommentCreate(commentActivity);
                break;
            case UPDATE_COMMENT:
                CommentUpdateActivityDTO commentUpdateActivity=JSON.parseObject(eventSource.getPayload(), CommentUpdateActivityDTO.class);
                params.setEvent(eventSource.getAction().getCode());
                params.setDiscussId(commentUpdateActivity.getDiscussId());
                params.setUid(commentUpdateActivity.getActionUid());
                params.setCreatedDate(commentUpdateActivity.getCreatedDate());
                discussActivityService.saveDiscussActivity(params);
                //发送通知
                notifyService.sendDiscussCommentUpdate(commentUpdateActivity);
                break;
            case CREATE_REPLY:
                BaseActivityDTO replyActivity=JSON.parseObject(eventSource.getPayload(), BaseActivityDTO.class);
                params.setEvent(eventSource.getAction().getCode());
                params.setDiscussId(replyActivity.getDiscussId());
                params.setUid(replyActivity.getActionUid());
                params.setCreatedDate(replyActivity.getCreatedDate());
                discussActivityService.saveDiscussActivity(params);
                break;
            case CLOSE:
                UpdateStateActivityDTO closeActivity=JSON.parseObject(eventSource.getPayload(), UpdateStateActivityDTO.class);
                params.setEvent(eventSource.getAction().getCode());
                params.setDiscussId(closeActivity.getDiscussId());
                params.setUid(closeActivity.getActionUid());
                params.setCreatedDate(closeActivity.getCreatedDate());
                discussActivityService.saveDiscussActivity(params);
                //发送状态变更通知
                notifyService.sendDiscussUpdateState(closeActivity);
                break;
            case OPEN:
                UpdateStateActivityDTO openActivity=JSON.parseObject(eventSource.getPayload(), UpdateStateActivityDTO.class);
                params.setEvent(eventSource.getAction().getCode());
                params.setDiscussId(openActivity.getDiscussId());
                params.setUid(openActivity.getActionUid());
                params.setCreatedDate(openActivity.getCreatedDate());
                discussActivityService.saveDiscussActivity(params);
                //发送状态变更通知
                notifyService.sendDiscussUpdateState(openActivity);
                break;
            case PIN:
                BaseActivityDTO pinActivity=JSON.parseObject(eventSource.getPayload(),BaseActivityDTO.class);
                params.setEvent(eventSource.getAction().getCode());
                params.setDiscussId(pinActivity.getDiscussId());
                params.setUid(pinActivity.getActionUid());
                params.setCreatedDate(pinActivity.getCreatedDate());
                discussActivityService.saveDiscussActivity(params);
                break;
            case UN_PIN:
                BaseActivityDTO unPinActivity=JSON.parseObject(eventSource.getPayload(),BaseActivityDTO.class);
                params.setEvent(eventSource.getAction().getCode());
                params.setDiscussId(unPinActivity.getDiscussId());
                params.setUid(unPinActivity.getActionUid());
                params.setCreatedDate(unPinActivity.getCreatedDate());
                discussActivityService.saveDiscussActivity(params);
                break;
            case PIN_CATEGORY:
                BaseActivityDTO pinCateActivity=JSON.parseObject(eventSource.getPayload(),BaseActivityDTO.class);
                params.setEvent(eventSource.getAction().getCode());
                params.setDiscussId(pinCateActivity.getDiscussId());
                params.setUid(pinCateActivity.getActionUid());
                params.setCreatedDate(pinCateActivity.getCreatedDate());
                discussActivityService.saveDiscussActivity(params);
                break;
            case UN_PIN_CATEGORY:
                BaseActivityDTO unPinCateActivity=JSON.parseObject(eventSource.getPayload(),BaseActivityDTO.class);
                params.setEvent(eventSource.getAction().getCode());
                params.setDiscussId(unPinCateActivity.getDiscussId());
                params.setUid(unPinCateActivity.getActionUid());
                params.setCreatedDate(unPinCateActivity.getCreatedDate());
                discussActivityService.saveDiscussActivity(params);
                break;
            /*case EDIT_PIN:
                BaseActivityDTO editPinActivity=JSON.parseObject(eventSource.getPayload(),BaseActivityDTO.class);
                params.setEvent(eventSource.getAction().getCode());
                params.setDiscussId(editPinActivity.getDiscussId());
                params.setUid(editPinActivity.getActionUid());
                params.setCreatedDate(editPinActivity.getCreatedDate());
                discussActivityService.saveDiscussActivity(params);
                break;*/
            case LOCK:
                UpdateStateActivityDTO lockActivity=JSON.parseObject(eventSource.getPayload(),UpdateStateActivityDTO.class);
                params.setEvent(eventSource.getAction().getCode());
                params.setDiscussId(lockActivity.getDiscussId());
                params.setUid(lockActivity.getActionUid());
                params.setCreatedDate(lockActivity.getCreatedDate());
                discussActivityService.saveDiscussActivity(params);
                //发送状态变更通知
                notifyService.sendDiscussUpdateState(lockActivity);
                break;
            case UN_LOCK:
                UpdateStateActivityDTO unLockActivity=JSON.parseObject(eventSource.getPayload(),UpdateStateActivityDTO.class);
                params.setEvent(eventSource.getAction().getCode());
                params.setDiscussId(unLockActivity.getDiscussId());
                params.setUid(unLockActivity.getActionUid());
                params.setCreatedDate(unLockActivity.getCreatedDate());
                discussActivityService.saveDiscussActivity(params);
                //发送状态变更通知
                notifyService.sendDiscussUpdateState(unLockActivity);
                break;

            case TRANSFER:
                BaseActivityDTO transferActivity=JSON.parseObject(eventSource.getPayload(),BaseActivityDTO.class);
                params.setEvent(eventSource.getAction().getCode());
                params.setDiscussId(transferActivity.getDiscussId());
                params.setUid(transferActivity.getActionUid());
                params.setCreatedDate(transferActivity.getCreatedDate());
                discussActivityService.saveDiscussActivity(params);
                break;
            case CREATE_ISSUE:
                BaseActivityDTO createIssueActivity=JSON.parseObject(eventSource.getPayload(),BaseActivityDTO.class);
                params.setEvent(eventSource.getAction().getCode());
                params.setDiscussId(createIssueActivity.getDiscussId());
                params.setUid(createIssueActivity.getActionUid());
                params.setCreatedDate(createIssueActivity.getCreatedDate());
                discussActivityService.saveDiscussActivity(params);
                break;
            case VOTE_DISCUSS:
                BaseActivityDTO voteActivity=JSON.parseObject(eventSource.getPayload(),BaseActivityDTO.class);
                params.setEvent(eventSource.getAction().getCode());
                params.setDiscussId(voteActivity.getDiscussId());
                params.setUid(voteActivity.getActionUid());
                params.setCreatedDate(voteActivity.getCreatedDate());
                discussActivityService.saveDiscussActivity(params);
                break;
            case COMMENT_AT:
                //调用通知服务，发送邮件通知
                DiscussAtActivityDTO atActivity=JSON.parseObject(eventSource.getPayload(), DiscussAtActivityDTO.class);
                EmailPushDiscussDTO notifyParams=new EmailPushDiscussDTO();
                BeanUtils.copyProperties(atActivity,notifyParams);
                //notifyFeignClient.emailPushDiscuss(notifyParams);
                break;
        }
    }
}
